package com.vr.cloudpicture.controller;

import com.vr.cloudpicture.common.BaseResponse;
import com.vr.cloudpicture.common.PageResult;
import com.vr.cloudpicture.common.ResultUtils;
import com.vr.cloudpicture.exception.ErrorCode;
import com.vr.cloudpicture.exception.ThrowUtils;
import com.vr.cloudpicture.model.dto.user.*;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import com.vr.cloudpicture.model.vo.UserVO;
import com.vr.cloudpicture.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制器
 *
 * @author hzh
 * @date 2025/04/27
 *
 *
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        return ResultUtils.success(userService.userRegister(userRegisterRequest));
    }
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public BaseResponse<String> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest==null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.userLogin(userLoginRequest,request));
    }
    @GetMapping("/get/login")
    @ApiOperation(value = "获取当前登录用户")
    public BaseResponse<UserLogin> getLoginUser(HttpServletRequest request) {
        return ResultUtils.success(userService.getLoginUser(request));
    }
    @PostMapping("/logout")
    @ApiOperation(value = "用户登出")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        return ResultUtils.success(userService.userLogout(request));
    }

    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取用户列表")
    public BaseResponse<PageResult<UserVO>> listUserByPage(@RequestBody UserQueryRequest userQueryRequest) {
        return ResultUtils.success(userService.pageQuery(userQueryRequest));
    }
    @PostMapping ("/add")
    @ApiOperation(value = "添加用户")
    public BaseResponse<String> addUser(@RequestBody UserAddRequest userAddRequest) {
        return ResultUtils.toBaseResponse(userService.saveUser(userAddRequest));
    }
    @PostMapping("/update")
    @ApiOperation(value = "更新用户")
    public BaseResponse<String> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        return ResultUtils.toBaseResponse(userService.updateUser(userUpdateRequest));
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    public BaseResponse<String> deleteUser(@RequestBody Long id) {
        return ResultUtils.toBaseResponse(userService.removeById(id));
    }
    @PostMapping("/exchange/vip")
    @ApiOperation(value = "兑换会员")
    public BaseResponse<Boolean> exchangeVip(@RequestBody VipExchangeRequest vipExchangeRequest,HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(userService.exchangeVip(vipExchangeRequest,loginUser));
    }
}
